package org.hive.data
{
    import flash.errors.IllegalOperationError;
    import flash.events.EventDispatcher;
    import flash.external.ExternalInterface;
    
    import org.hive.utils.FLHTML;

    public class XMLDOM extends EventDispatcher
    {
        public function XMLDOM()
        {
            super(null);
        }
        
        
        //________________________________________________ PARSING METHODS
        public function parseNode( id:String ):void
        {
            if( ExternalInterface.available )
            parse(ExternalInterface.call(JS_METHODS.getElementById,id));
            else throw new IllegalOperationError('allowScriptAccess must be set to either "samedomain" or "always" in order to use this method."');
        }
        
        private var _data:XML;
        public function getData():XML
        {
            return _data;
        }
        
        public function parse( value:String ):XML
        {
            try{
                _data = XML('<hive>'+FLHTML.adapt(value)+'</hive>');
            }
            catch(e:TypeError)
            {
                trace(e);
            }
            return _data;
        }
        
        
        //________________________________________________ JAVASCRIPT METHODS
        private const JS_METHODS:XML = 
        <methods>
            <getElementById><![CDATA[
            function(id)
            {
                return document.getElementById(id).innerHTML;
            }
            ]]></getElementById>
        </methods>;
        
    }
}